import Snake from "./Snake";
import Score from "./Score";
import Food from "./Food";

class GameControl {
  //定义三个属性 类也可以当成属性
  snake: Snake;
  score: Score;
  food: Food;
  //创建一个属性控制蛇的移动方向 键盘的字面量
  direction: string = "";
  //记录游戏是否结束
  isLive: boolean = true;

  constructor() {
    this.snake = new Snake();
    this.score = new Score();
    this.food = new Food();
    this.init();
  }
  //游戏开始的方法
  init() {
    //绑定键盘事件
    document.addEventListener("keydown", this.keydoenHandler.bind(this));
    this.move();
    // document.addEventListener("keydown", this.keydoenHandler);
  }
  //键盘函数 KeyboardEvent类型为键盘事件如click
  keydoenHandler(event: KeyboardEvent) {
    let B = this.snake.bodies[1];
    console.log(event.key);

    console.log(this.direction);

    //需要检测用户是否按了正确所需按键
    if (B) {
      switch (event.key) {
        case "ArrowUp":
        case "Up":
          if (this.direction === "ArrowDown") {
            this.direction = event.key.includes("Arr") ? "ArrowDown" : "Down";
          } else {
            this.direction = event.key;
          }
          break;
        case "ArrowDown":
        case "Down":
          if (this.direction === "ArrowUp") {
            this.direction = event.key.includes("Arr") ? "ArrowUp" : "Up";
          } else {
            this.direction = event.key;
          }
          break;
        case "ArrowLeft":
        case "Left":
          if (this.direction === "ArrowRight") {
            this.direction = event.key.includes("Arr") ? "ArrowRight" : "Right";
          } else {
            this.direction = event.key;
          }
          break;
        case "ArrowRight":
        case "Right":
          if (this.direction === "ArrowLeft") {
            this.direction = event.key.includes("Arr") ? "ArrowLeft" : "Left";
          } else {
            this.direction = event.key;
          }
          break;
      }
    } else {
      this.direction = event.key;
    }

    //console.log(event.key); //输出键盘的的字面量
    // this.direction = event.key;
  }
  //   keydoenHandler = (event: KeyboardEvent) => {
  //     console.log(event.key); //输出键盘的的字面量
  //     this.direction = event.key;
  //   };
  //蛇移动的方法
  move() {
    let X = this.snake.X;
    let Y = this.snake.Y;

    //   根据方向使得蛇的位置xy改变
    switch (this.direction) {
      case "ArrowUp":
      case "Up":
        //向上移动
        Y -= 10;
        break;
      case "ArrowDown":
      case "Down":
        //向下移动
        Y += 10;
        break;
      case "ArrowLeft":
      case "Left":
        //向左移动
        X -= 10;
        break;
      case "ArrowRight":
      case "Right":
        //向右移动
        X += 10;
        break;
    }
    this.checkEat(X, Y);
    //   //食物重置
    //   this.food.change();
    //   //分数增加
    //   this.score.addScore();
    //   //蛇增加一个
    //   this.snake.addBody();

    //如果异常了用try catch捕获异常
    try {
      //检查蛇是否吃到了食物
      this.snake.X = X;
      this.snake.Y = Y;
    } catch (error) {
      alert(error);
      this.isLive = false;
    }
    // this.snake.X = X;
    // this.snake.Y = Y;
    this.isLive &&
      setTimeout(this.move.bind(this), 300 - (this.score.level - 1) * 30);
  }
  //定义一个方法检查是否吃到了食物
  checkEat(X: number, Y: number) {
    if (X === this.food.X && Y === this.food.Y) {
      //蛇增加一个
      this.snake.addBody();
      //食物重置
      this.food.change();
      //分数增加
      this.score.addScore();
    }
  }
}

export default GameControl;
